#include "widget.h"
#include "ui_widget.h"
#include <QPainter>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("设置背景色");

    //1
//    m_pWidget = new QWidget(this);
//    m_pWidget->setGeometry(50, 50, 300, 100);
//    QPalette pal(m_pWidget->palette());
//    //设置背景黑色
//    pal.setColor(QPalette::Background, Qt::blue);
//    m_pWidget->setAutoFillBackground(true);
//    m_pWidget->setPalette(pal);
//    m_pWidget->show();

    //2
//    m_pWidget = new QWidget(this);
//    m_pWidget->setGeometry(50, 50, 300, 100);
//    m_pWidget->setStyleSheet("background-color:green;");
//    m_pWidget->show();
}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter p(this);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::red);
    p.drawRect(rect());
}

Widget::~Widget()
{
    delete ui;
}
